package io.renren.modules.archive.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.renren.common.utils.R;
import io.renren.modules.archive.entity.ArchiveCollectEntity;
import io.renren.modules.archive.entity.NdJgMenuEntity;
import io.renren.modules.archive.service.ArchiveCollectService;
import io.renren.modules.archive.service.NdJgMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("archive/ndjgmenu")
public class NdJgMenuController {

    @Autowired
    private ArchiveCollectService archiveCollectService;
    @Autowired
    private NdJgMenuService ndJgMenuService;
    /**
     * 档案采集权限控制菜单
     */
    @GetMapping("/getNdJgTreeList")
    public R getNdJgTreeList(@RequestParam Map<String, Object> params){
        List<ArchiveCollectEntity> menuList = archiveCollectService.getArchivecollectList();
        List<Long> ndJgMenuList=ndJgMenuService.getNdJgMenuList(params);
        List<Long> leafMenuIds=extractLeafMenuIds(menuList,ndJgMenuList);
        return R.ok().put("menuList", menuList)
                .put("ndJgMenuList",ndJgMenuList)
                .put("leafMenuIds",leafMenuIds)
                ;
    }



    /**
     * 档案采集权限控制菜单
     */
    @GetMapping("/getQxList")
    public R getQxList(@RequestParam Map<String, Object> params){

        List<Long> ndJgMenuList=ndJgMenuService.getNdJgLevelMenuList(params);
        List<ArchiveCollectEntity> menuList=archiveCollectService.getQxList(ndJgMenuList);

        return R.ok().put("menuList", menuList) ;


    }






    /**
     * 更新年份机构档案采集菜单关联
     */
    @PostMapping("/updateNdJgMenu")
    public R updateNdJgMenu(@RequestBody NdJgMenuEntity ndJgMenuEntity){


        ndJgMenuService.updateNdJgMenu(ndJgMenuEntity);

        return R.ok();
    }


    public List<Long> extractLeafMenuIds(List<ArchiveCollectEntity> menuList, List<Long> ndJgMenuList) {
        Set<Long> ndJgMenuSet = new HashSet<>(ndJgMenuList); // 转换为集合以加速查找
        List<Long> leafMenuIds = new ArrayList<>();

        for (ArchiveCollectEntity entity : menuList) {
            collectLeafMenuIds(entity, ndJgMenuSet, leafMenuIds);
        }

        return leafMenuIds;
    }

    private void collectLeafMenuIds(ArchiveCollectEntity entity, Set<Long> ndJgMenuSet, List<Long> leafMenuIds) {
        if (entity.isLeaf()) { // 如果当前节点是叶子节点
            if (ndJgMenuSet.contains(entity.getMenuId())) { // 并且它的menuId存在于ndJgMenuList中
                leafMenuIds.add(entity.getMenuId()); // 则将其menuId添加到结果列表中
            }
        } else if (entity.getList() != null) { // 如果当前节点有子节点
            for (ArchiveCollectEntity child : entity.getList()) { // 则递归处理每个子节点
                collectLeafMenuIds(child, ndJgMenuSet, leafMenuIds);
            }
        }
    }
}
